package com.call.project.utils;

import com.call.project.entity.Meeting;
import com.call.project.entity.SerialPort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 Created by IntelliJ IDEA.
 User: wk
 Date: 2017-03-22 下午3:41
 */
public class ConfigUtils {
    private final static String               FILE_PATH           = "config.data";
    public final static String                MEETING_CONFIG      = "metting.data";
    private static final Logger               LOGGER              = LoggerFactory.getLogger(ConfigUtils.class);
    private static File                       CONFIGFILE          = null;
    private static File                       MEETING_CONFIG_FILE = null;
    public static final Map<Integer, String> BUTTON_MSG          = new HashMap<>();
    static {
        CONFIGFILE = new File(FILE_PATH);
        MEETING_CONFIG_FILE = new File(MEETING_CONFIG);
        BUTTON_MSG.put(0, "会议结束");
        BUTTON_MSG.put(1, "茶水服务");
        BUTTON_MSG.put(2, "其他服务");
        BUTTON_MSG.put(3, "系统故障");
    }

    public static void checkNotAndCreate() {
        //校验串口配置
        if (!checkSeriaPortExists()) {
            writeDefaultSeriaPort();
        }
        if (!checkMettingExists()) {
            writeDefaultMetting();
        }
    }

    private static void writeDefaultMetting() {
        writeObject(getDefaultMeeting(), MEETING_CONFIG_FILE);
    }

    public static boolean checkMettingExists() {
        return MEETING_CONFIG_FILE.exists();
    }

    public static boolean checkSeriaPortExists() {
        return CONFIGFILE.exists();
    }

    public static SerialPort getSeriaPort() {
        return getObject(CONFIGFILE);
    }

    public static void writeMeeting(HashMap<Integer, Meeting> data) {
        writeObject(data, MEETING_CONFIG_FILE);
    }

    public static HashMap<Integer, Meeting> getMeeting() {
        return getObject(MEETING_CONFIG_FILE);
    }

    public static List<Meeting> getMeetingList() {
        HashMap<Integer, Meeting> meetingHashMap = getObject(MEETING_CONFIG_FILE);
        List<Meeting> meetingList = new ArrayList<>();
        if (meetingHashMap != null) {
            for (Map.Entry<Integer, Meeting> integerMeetingEntry : meetingHashMap.entrySet()) {
                meetingList.add(integerMeetingEntry.getValue());
            }
        }
        return meetingList;
    }

    public static HashMap<Integer, Meeting> getDefaultMeeting() {
        HashMap<Integer, Meeting> meetingHashMap = new HashMap<>();
        for (int i = 1; i <= 8; i++) {
            Meeting meeting = new Meeting(i + "号会议室", i);
            meetingHashMap.put(i, meeting);
        }
        return meetingHashMap;
    }

    public static SerialPort getDefaultSeriaPort() {
        SerialPort serialPort = new SerialPort();
        serialPort.setPortNumber(3);
        serialPort.setPartity('N');
        serialPort.setDatabits(8);
        serialPort.setStopbits(1);
        serialPort.setRate(1200);
        return serialPort;
    }

    public static void writeDefaultSeriaPort() {
        writeObject(getDefaultSeriaPort(), CONFIGFILE);
    }

    public static <T> T getObject(File file) {
        ObjectInputStream objectInputStream = null;
        Object obj = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream(file));
            obj = objectInputStream.readObject();
        } catch (Exception e) {
            LOGGER.error("getObject", e);
            obj = null;
        } finally {
            if (objectInputStream != null)
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return (T) obj;

    }

    public static <T> void writeObject(T data, File file) {
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
            objectOutputStream.writeObject(data);
            objectOutputStream.flush();
        } catch (Exception e) {
            LOGGER.error("writeObject", e);
        } finally {
            if (objectOutputStream != null)
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    LOGGER.error("writeObject", e);
                }
        }
    }
}
